//
//  AnswerViewController.m
//  TestSchool
//
//  Created by wuxiaoquan on 12-11-2.
//  Copyright (c) 2012年 yaoxia. All rights reserved.
//

#import "AnswerViewController.h"
#import "UserManager.h"
#import "CompleteViewController.h"
#import "AppDelegate.h"

@interface AnswerViewController ()

@end

@implementation AnswerViewController

@synthesize answerView,questionList,testId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.answerView becomeFirstResponder];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Send" style:UIBarButtonItemStylePlain target:self action:@selector(submit:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backToQuestion:)];
        self.title = @"Answer";
    self.answerView.text = [UserManager getCurrentUser].currentAnswer;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.answerView = nil;
    self.questionList = nil;
    request = nil;
    self.testId = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)submit:(id)sender
{
    NSLog(@"发送答案");
    [self showWaitView:nil];
    
    //NSString * URL_LOGIN = @"http://218.24.94.212/Test/answerone.jsp";
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    NSString * URL_LOGIN = [NSString stringWithFormat:@"%@/Test/answerone.jsp",delegate.baseUrl];
    
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:self.testId forKey:@"testid"];
    [request setPostValue:[[self.questionList objectAtIndex:[UserManager getCurrentUser].currentQuestionIndex] objectForKey:@"questionid"] forKey:@"questionid"];
    [request setPostValue:[UserManager getCurrentUser].userId forKey:@"userid"];
    [request setPostValue:self.answerView.text forKey:@"answercontent"];
    
    [request setCompletionBlock:^{
        [self hideWaitView];
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
        //NSLog(@"responseString = %@",responseString);//显示收到的信息
        
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"Message"
                                          message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            return ;
        }
        
        [[self.questionList objectAtIndex:[UserManager getCurrentUser].currentQuestionIndex] setObject:@"1" forKey:@"answered"];
        [UserManager getCurrentUser].currentAnswer = @"";
        
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Message"
                              message:@"Send answer success!"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        
        if ([UserManager getCurrentUser].currentQuestionIndex == self.questionList.count - 1) {
            //已经到最后一个了
            CompleteViewController *detailController = [[CompleteViewController alloc]initWithNibName:@"CompleteViewController" bundle:nil];
            //[self.navigationController popViewControllerAnimated:YES];
            [self.navigationController pushViewController:detailController animated:YES];
        }
        else
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Network error"
                              message:@"Please check your network"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
    
}

-(IBAction)backToQuestion:(id)sender
{
    [UserManager getCurrentUser].currentAnswer = self.answerView.text;
    [self.navigationController popViewControllerAnimated:YES];
}

@end
